#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int a[1024];

void print(int n)
{
    int i;

    for (i = 0; i < n; i++) {
        if (a[i] > 0)
            printf("%d ", a[i]);
    }
    printf("\n");
}

int check(int i, int j, int n)
{
    int k, sum;

    sum = 0;
    for (k = 0; k < i; k++) {
        if (j > a[k])
            return 0;
        sum += a[k];
    }
    sum += j;
    
    if (sum > n)
        return 0;
    else if (sum != n && i == n-1)
            return 0;
    else {
        a[i] = j;
        return 1;
    }
}

int split(int n)
{
    int i, j, find, total;
    int *choosed = malloc(sizeof(int)*n);
    
    memset(choosed, 0xff, sizeof(int)*n);
    total = 0;
    for (i = 0; i < n; i++) {
        find = 0;
        for (j = choosed[i]+1; j < n; j++) {
            if (check(i, j, n)) {
                a[i] = j;
                choosed[i] = j;
                find = 1;

                if (i == n-1) {
                    print(n);
                    total ++;
                    find = 0;
                }

                break;
            }
        }
        
        if (find == 0) {
            if (i == 0) {
                free(choosed);
                return total;
            }
            else {
                choosed[i] = -1;
                i -= 2;
            }
        }
    }

}

int split_2(int n)
{
    int a[n+1], b[n+1], i, j, k, t;
    
    if (n == 1)
        return 0;

    for (i = 0; i < n+1; i++) {
        a[i] = 1;
        b[i] = 0;
    }

    for (i = 2; i < n+1; i++) {
        for (j = 0; j < n+1; j++) {
            for (k = 0; k+j < n+1; k += i) {
                b[k+j] += a[j]; 
            }
        }

        for (j = 0; j < n+1; j++) {
            a[j] = b[j];
            b[j] = 0;
        }
    }

    return a[n]-1;
}

int split_3(int n)
{
    int a[n], i, count, x, y, s;
    
    count = 1;
    for (i = 0; i < n; i++)
        a[i] = 1;

    i = n-1;
    while (i > 0) {
        x = a[i--];
        y = a[i--];
        x = x+y;
        s = x/(y+1);
        if (x%(y+1) > 0 && x/(y+1) == 1)
            a[++i] = x;
        else {
            while (s-- > 1)
                a[++i] = y+1;
            a[++i] = y+1+x%(y+1);
        }
        count ++;
    }
    
    return count-1;
}

int main(int argc, char *argv[])
{
    int n = 10;
    printf("split of %d is %d\n", n, split(n));
    printf("split of %d is %d\n", n, split_2(n));
    printf("split of %d is %d\n", n, split_3(n));
    return 0;
}
